package com.haima.ziliao.controller;

import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.servlet.http.HttpServletResponse;

import com.haima.common.core.redis.RedisCache;
import com.haima.common.utils.DateUtils;
import com.haima.utils.WxTool;
import com.haima.ziliao.domain.PZiliao;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.haima.common.annotation.Log;
import com.haima.common.core.controller.BaseController;
import com.haima.common.core.domain.AjaxResult;
import com.haima.common.enums.BusinessType;
import com.haima.ziliao.domain.PDuwu;
import com.haima.ziliao.service.IPDuwuService;
import com.haima.common.utils.poi.ExcelUtil;
import com.haima.common.core.page.TableDataInfo;

/**
 * 读物资料Controller
 * 
 * @author hima
 * @date 2024-11-14
 */
@RestController
@RequestMapping("/ziliao/duwu")
public class PDuwuController extends BaseController
{
    @Autowired
    private IPDuwuService pDuwuService;
    @Autowired
    private RedisCache redisCacheService;
    /**
     * 查询读物资料列表
     */
    @PreAuthorize("@ss.hasPermi('ziliao:duwu:list')")
    @GetMapping("/list")
    public TableDataInfo list(PDuwu pDuwu)
    {
        startPage();
        List<PDuwu> list = pDuwuService.selectPDuwuList(pDuwu);
        return getDataTable(list);
    }

    @GetMapping("/getDwLink")
    public AjaxResult getDwLink(PDuwu pDuwu)
    {
        PDuwu dw = pDuwuService.selectPDuwuById(pDuwu.getId());
        if(redisCacheService.hasKey("duwu"+pDuwu.getId())){
            return AjaxResult.success("ok",redisCacheService.getCacheObject("duwu"+pDuwu.getId()));
        }
        String page = "pages/duwu/detail/index?id="+pDuwu.getId();
        String title = pDuwu.getTitle();

        String link = WxTool.getShortLink(page,title);

        String copyLink = "名称："+title +"\n"+"链接："+link;
        redisCacheService.setCacheObject("duwu"+pDuwu.getId(),copyLink,29, TimeUnit.DAYS);
        return AjaxResult.success("ok",copyLink);
    }

    @GetMapping("/getTuijian")
    public TableDataInfo getTuijian(PDuwu pDuwu)
    {
        pDuwu.setTuijian("1");
        startPage();
        List<PDuwu> list = pDuwuService.selectPDuwuList(pDuwu);
        return getDataTable(list);
    }
    @GetMapping("/getNews")
    public TableDataInfo getNews(PDuwu pDuwu)
    {
        startPage();
        List<PDuwu> list = pDuwuService.getNews(pDuwu);
        return getDataTable(list);
    }

    @GetMapping("/getListByFid")
    public TableDataInfo getListByFid(PDuwu pDuwu)
    {
        startPage();
        pDuwu.setTid(pDuwu.getTid());
        List<PDuwu> list = pDuwuService.getNews(pDuwu);
        return getDataTable(list);
    }

    @GetMapping("/detail")
    public AjaxResult detail(PDuwu pDuwu)
    {
        PDuwu dw = pDuwuService.selectPDuwuById(pDuwu.getId());
        return AjaxResult.success(dw);
    }

    /**
     * 导出读物资料列表
     */
    @PreAuthorize("@ss.hasPermi('ziliao:duwu:export')")
    @Log(title = "读物资料", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PDuwu pDuwu)
    {
        List<PDuwu> list = pDuwuService.selectPDuwuList(pDuwu);
        ExcelUtil<PDuwu> util = new ExcelUtil<PDuwu>(PDuwu.class);
        util.exportExcel(response, list, "读物资料数据");
    }

    /**
     * 获取读物资料详细信息
     */
    @PreAuthorize("@ss.hasPermi('ziliao:duwu:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(pDuwuService.selectPDuwuById(id));
    }

    /**
     * 新增读物资料
     */
    @PreAuthorize("@ss.hasPermi('ziliao:duwu:add')")
    @Log(title = "读物资料", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PDuwu pDuwu)
    {
        pDuwu.setAddTime(DateUtils.getNowDate());
        return toAjax(pDuwuService.insertPDuwu(pDuwu));
    }

    /**
     * 修改读物资料
     */
    @PreAuthorize("@ss.hasPermi('ziliao:duwu:edit')")
    @Log(title = "读物资料", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PDuwu pDuwu)
    {
        return toAjax(pDuwuService.updatePDuwu(pDuwu));
    }

    /**
     * 删除读物资料
     */
    @PreAuthorize("@ss.hasPermi('ziliao:duwu:remove')")
    @Log(title = "读物资料", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(pDuwuService.deletePDuwuByIds(ids));
    }
}
